﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;
using System.Collections;

using SuperMapLib;

namespace JR_RouteAlarm.MakingBuffer
{
    class Common : IRoute
    {
        /*変数*/
        protected readonly soDatasets objDSs = null;
        protected readonly soStrings objStrFields = null;

        protected string feature = String.Empty;
        protected string daibun = String.Empty;
        protected string tsuusho = String.Empty;

        private readonly Hashtable routeFtrWithInfo = null; // 地物と情報ペア
        private readonly Hashtable routeLnWithBfr = null;   // 線路とバッファーペア

        public Common(soDatasets objDSs, Hashtable routeLnWithBfr, soStrings objStrFields)
        {
            this.objDSs = objDSs;
            this.routeLnWithBfr = routeLnWithBfr;
            this.objStrFields = objStrFields;
            routeFtrWithInfo = new Hashtable();
        }

        /// <summary>
        /// 指定するキロ程で線路を分けてバッファーを作成
        /// </summary>
        /// <param name="objFullRoute">対象線路</param>
        /// <param name="objDVs">通じているキロ程データセットベクトル</param>
        protected void MakeBufferForPart(soGeoLine objFullRoute, params soDatasetVector[] objDVs)
        {
            soGeoPoint[] objPointsDividing = null;

            objPointsDividing = new soGeoPoint[Utility.MAX_CROSSING_POINTS];
            SetAreaInfo(objPointsDividing, objFullRoute, objDVs);

            int lineCount = 0;
            //if (objPointsDividing[0] != null)
            if (objPointsDividing[
                (feature + tsuusho == "0102650" && objFullRoute.Length < 80000)
                    || feature + tsuusho == "0102463"   // 京浜東北根岸線
                    || feature + tsuusho == "0102481"   // 常磐線
                    || feature + tsuusho == "0102260"   // 中央・総武緩行線
                    ? 1 : 0] != null)
            {
                //MakeDividingBuffer(objFullRoute, objPointsDividing, 0, ref lineCount);
                MakeDividingBuffer(objFullRoute, objPointsDividing,
                    // 成田(松岸)線
                    (feature + tsuusho == "0102650" && objFullRoute.Length < 80000)
                    || feature + tsuusho == "0102463"   // 京浜東北根岸線
                    || feature + tsuusho == "0102481"   // 常磐線
                    || feature + tsuusho == "0102260"   // 中央・総武緩行線
                    ? 1 : 0, ref lineCount);
            }
        }

        /// <summary>
        /// エリアコード別より、バッファーに情報を入れる
        /// </summary>
        /// <param name="objPointsKirotei">分けるようにキロ程ポイント集合</param>
        /// <param name="objRoute">対象線路</param>
        /// <param name="objDVs">通じているキロ程データセットベクトル</param>
        private void SetAreaInfo(soGeoPoint[] objPointsKirotei, soGeoLine objRoute, soDatasetVector[] objDVs)
        {
            soRecordset objRSArea = null;
            soDatasetVector objVectorArea = null;
            string strSqlArea = String.Empty;

            //エリア境界の属性テーブルより、エリアに応じて情報を取り出してセット
            objVectorArea = objDSs[Utility.PROPERTY_TABLE_NAME] as soDatasetVector;
            Console.WriteLine("Dataset name:" + objDVs[0].TableName);
            if (objDVs[0].TableName.Contains("千葉") && objDVs.Length == 2 && objDVs[1] == null)  //総武
            {
                strSqlArea = String.Format(
                    "{0} = '01' and (({1} = '{3}' and {2} = '{4}') or ({1} = '{5}' and {2} = '{6}'))"
                    + " and area_cd not like 'NM*'",
                    RouteDataset.FLD_DAIBUN_CD, RouteDataset.FLD_FEATURE_CD, RouteDataset.FLD_TSUUSHO_CD,
                    //総武本線
                    "0102", "650",
                    //総武快速線
                    "0103", "662");
            }
            else if (objDVs[0].TableName.Contains("高崎") && objDVs.Length == 3)
            {
                strSqlArea = String.Format(
                    "{0} = '01' and (({1} = '{3}' and {2} = '{4}') or ({1} = '{5}' and {2} = '{6}'))",
                    RouteDataset.FLD_DAIBUN_CD, RouteDataset.FLD_FEATURE_CD, RouteDataset.FLD_TSUUSHO_CD,
                    //上越線
                    "0106", "495",
                    //高崎線
                    "0102", "493");
            }
            else if (objDVs.Length == 5)    //武蔵野線
            {
                strSqlArea = String.Format("{0} = '01' and (({1} in ('{3}', '{4}') and {2} = '{5}') or ({1} = '{3}' and {2} = '{6}'))",
                    RouteDataset.FLD_DAIBUN_CD, RouteDataset.FLD_FEATURE_CD, RouteDataset.FLD_TSUUSHO_CD, 
                    "0102", 
                    //その１
                    "0106", "201",
                    //その２
                    "666");
            }
            else if (objDVs[0].TableName.Contains("千葉") && objDVs.Length == 1
                && feature + tsuusho == "0102650")  //成田(松岸)線
            {
                strSqlArea = String.Format("{0} = '01' and {1} = '{3}' and {2} = '{4}' and {5} like 'NM*'",
                    RouteDataset.FLD_DAIBUN_CD, RouteDataset.FLD_FEATURE_CD, RouteDataset.FLD_TSUUSHO_CD, 
                    "0102", "650", RouteArea.FLD_AREA_CD);
            }
            else if (feature + tsuusho == "0102480")  //常磐緩行線
            {
                strSqlArea = String.Format("{0} = '{1}' and {2} = '{3}' and {4} = '{5}' "
                    + " and (right(area_cd, 2) > 9 or right(area_cd, 1) > 8)",
                    RouteDataset.FLD_FEATURE_CD, feature,
                    RouteDataset.FLD_DAIBUN_CD, daibun,
                    RouteDataset.FLD_TSUUSHO_CD, "481");
            }
            else   //その他
            {
                strSqlArea = String.Format("{0} = '{1}' and {2} = '{3}' and {4} = '{5}' ",
                    RouteDataset.FLD_FEATURE_CD, feature,
                    RouteDataset.FLD_DAIBUN_CD, daibun,
                    RouteDataset.FLD_TSUUSHO_CD, tsuusho);
            }
            objRSArea = objVectorArea.Query(strSqlArea, false, null, "");
            if (objRSArea != null && objRSArea.RecordCount > 0)
            {
                if (!objRSArea.IsBOF())
                {
                    objRSArea.MoveFirst();
                }

                //デフォルトは、順序でエリアコードずつ情報をセット
                RouteArea routeInfo = null;
                ArrayList multiRoutes = null;
                soRecordset objRSKirotei = null;
                multiRoutes = new ArrayList();
                string strSqlKirotei = String.Empty;
                for (; !objRSArea.IsEOF(); objRSArea.MoveNext())
                {
                    routeInfo = new RouteArea();
                    int kirotei = 0;

                    routeInfo.AreaID = objRSArea.GetFieldValueText(RouteArea.FLD_AREA_CD);
                    routeInfo.BeginKirotei =
                        Int32.TryParse(objRSArea.GetFieldValueText(RouteArea.FLD_KIROTEI_BGN), out kirotei) ?
                        Convert.ToInt32(objRSArea.GetFieldValueText(RouteArea.FLD_KIROTEI_BGN)) : -1;
                    if (routeInfo.BeginKirotei > -1)
                    {
                        strSqlKirotei += String.Format("{0},", routeInfo.BeginKirotei);
                    }
                    routeInfo.EndKirotei =
                        Int32.TryParse(objRSArea.GetFieldValueText(RouteArea.FLD_KIROTEI_END), out kirotei) ?
                        Convert.ToInt32(objRSArea.GetFieldValueText(RouteArea.FLD_KIROTEI_END)) : -1;

                    for (int i = 0; i < Utility.MAX_ALARM_TARGETS; i++)
                    {
                        routeInfo.alarmRoute[i] = objRSArea.GetFieldValueText(RouteArea.FLD_ALARM_ROUTE + (i + 1).ToString());
                        routeInfo.alarmArea[i] = objRSArea.GetFieldValueText(RouteArea.FLD_ALARM_AREA + (i + 1).ToString());
                    }

                    multiRoutes.Add(routeInfo);
                }
                objRSArea.Close();
                Marshal.ReleaseComObject(objRSArea);
                objRSArea = null;

                strSqlKirotei = strSqlKirotei.TrimEnd().TrimEnd(',');
                // 一時用　横須賀線
                strSqlKirotei = routeInfo.AreaID.Contains("SK") ?
                    strSqlKirotei.Replace("6100", "6200") : strSqlKirotei;
                // 一時用　東北海道線
                strSqlKirotei = routeInfo.AreaID.Contains("TF") ?
                    strSqlKirotei.Replace("10400", "10700") : strSqlKirotei;

                //通じているキロ程より、エリアコードずつキロ程ポイントを見当たる
                int idx = 0;
                soDatasetVector objVector = null;
                string strTsuusho = tsuusho;
                for (int i = 0; i < objDVs.Length && objDVs[i] != null; i++)
                {
                    objVector = objDVs[i];
                    Console.WriteLine(i.ToString() + "\t---" + objDVs[i].TableName);
                    string strRouteDiff = String.Empty;
                    if (routeInfo.AreaID.Contains("SA"))    // 埼京線
                    {
                        if (objVector.TableName.Contains("大宮"))
                        {
                            strTsuusho = tsuusho == "476" ? "468" : tsuusho;
                        }
                        else if (objVector.TableName.Contains("東京"))
                        {
                            strRouteDiff = String.Format(" or {0} = '468' ", RouteDataset.FLD_TSUUSHO_CD);
                        }
                    }
                    else if (routeInfo.AreaID.Contains("NA"))   // 成田(我孫子)線
                    {
                        strTsuusho = "662";
                    }
                    else if (routeInfo.AreaID.Contains("HF"))   //東北貨物線
                    {
                        strTsuusho = "465";
                    }
                    else if (routeInfo.AreaID.Contains("SR") || routeInfo.AreaID.Contains("SH"))
                    {
                        strTsuusho = strTsuusho == "662" ? "650" : strTsuusho;
                        strRouteDiff = String.Format(" or {0} = '655' or {0} = '649'", RouteDataset.FLD_TSUUSHO_CD);
                    }
                    else if (objDVs[0].TableName.Contains("高崎") && objDVs.Length == 3)
                    {
                        if (routeInfo.AreaID.Contains("JO") || routeInfo.AreaID.Contains("TS"))
                        {
                            strRouteDiff = String.Format(" or {0} = '{1}'",
                                RouteDataset.FLD_TSUUSHO_CD, strTsuusho == "493" ? "495" : strTsuusho);
                        }
                    }
                    else if (routeInfo.AreaID.Contains("MS"))   // 武蔵野線
                    {
                        strRouteDiff = String.Format(" or {0} = '{1}'",
                            RouteDataset.FLD_TSUUSHO_CD, strTsuusho == "666" ? "201" : strTsuusho == "201" ? "666" : strTsuusho);
                    }
                    else if (routeInfo.AreaID.Contains("YD"))   // 山手線
                    {
                        strRouteDiff = String.Format(" or {0} in ('{1}', '{2}') and {3} in ('{4}', '{5}')",
                            RouteDataset.FLD_TSUUSHO_CD, "462", "116", RouteDataset.FLD_FEATURE_CD, "0301", "0302");
                    }
                    else if (routeInfo.AreaID.Contains("SK"))   // 横須賀線
                    {
                        strRouteDiff = String.Format(" or {0} in ('{1}', '{2}') and {3} = '{4}'",
                            RouteDataset.FLD_TSUUSHO_CD, "155", "170", RouteDataset.FLD_FEATURE_CD, "0302");
                    }
                    else if (routeInfo.AreaID.Contains("TF"))   // 東海道貨物線
                    {
                        strRouteDiff = String.Format(" or {0} in ('{1}', '{2}') and {3} = '{4}'",
                            RouteDataset.FLD_TSUUSHO_CD, "155", "111", RouteDataset.FLD_FEATURE_CD, "0302");
                    }
                    else if (routeInfo.AreaID.Contains("KE"))   // 京浜東北根岸線
                    {
                        //strRouteDiff = String.Format("({0} = '{4}' and {1} = '{2}' and {3} in ({5})) "
                        //    + " or ({0} = '{6}' and {1} = '{2}' and {3} in ({7}))"
                        //    + " or ({0} = '{8}' and {1} = '{2}' and {3} in ({9}))"
                        //    + " or ({0} = '{10}' and {1} = '{2}' and {3} in ({11}))",
                        //    RouteDataset.FLD_TSUUSHO_CD, RouteDataset.FLD_FEATURE_CD, "0302", RouteArea.FLD_KIROTEI,
                        //    "461", "30300,29200,27600,25200,23800,21400,15000",   // 王子以降、処理対象は川口(15000)以降→キロ標_大宮の用意
                        //    tsuusho, "10400,9000,8200,7300,6600,6000,5000,3200,3000,2100,800,700",  // 東京～赤羽(10400)→キロ標_東京の用意
                        //    "117", "1300,5000,5600,7200,9200,9800,9900,16900,18300,19000,20400,20600,23300,23500,24200,24400,28500",    // 東京～蒲田(16900)→キロ標_東京の用意
                        //    "173", "1400,19200");
                        if (objDVs[i].TableName.Contains("大宮"))   // キロ標_大宮
                        {
                            strRouteDiff = String.Format("({0} = '{4}' and {1} = '{2}' and {3} in ({5})) ",
                                RouteDataset.FLD_TSUUSHO_CD, RouteDataset.FLD_FEATURE_CD, "0302", RouteArea.FLD_KIROTEI,
                                "461", "29200,27600,25200,23800,21400,15000");   // 王子以降、処理対象は川口(15000)以降
                        }
                        else if (objDVs[i].TableName.Contains("東京"))  // キロ標_東京
                        {
                            strRouteDiff = String.Format("({0} = '{4}' and {1} = '{2}' and {3} in ({5})) "
                                + " or ({0} = '{6}' and {1} = '{2}' and {3} in ({7}))",
                                RouteDataset.FLD_TSUUSHO_CD, RouteDataset.FLD_FEATURE_CD, "0302", RouteArea.FLD_KIROTEI,
                                tsuusho, "10400,9000,8200,7300,6600,6000,5000,3200,3000,2100,800,700",    // 東京～赤羽(10400)
                                "117", "1300,5000,5600,7200,9200,9800,9900,16900"); // 東京～蒲田(16900)
                        }
                        else if (objDVs[i].TableName.Contains("横浜"))  // キロ標_横浜
                        {
                            strRouteDiff = String.Format("({0} = '{4}' and {1} = '{2}' and {3} in ({5})) "
                                +" or ({0} = '{6}' and {1} = '{2}' and {3} in ({7}))",
                                RouteDataset.FLD_TSUUSHO_CD, RouteDataset.FLD_FEATURE_CD, "0302", RouteArea.FLD_KIROTEI,
                                "117", "18300,19000,20400,20600,23300,23500,24200,24400,28500",    // 川崎(18300)～東神奈川(28500)
                                "173", "1400,19200");   // 横浜以降
                        }
                    }
                    else if (routeInfo.AreaID.Contains("NM"))   // 成田(松岸)線
                    {
                        strRouteDiff = String.Format(" or {0} in ('{1}', '{2}') and {3} = '{4}'",
                            RouteDataset.FLD_TSUUSHO_CD, "662", "663", RouteDataset.FLD_FEATURE_CD, "0302");
                        strTsuusho = "000";
                    }
                    else if (routeInfo.AreaID.Contains("JB") && multiRoutes.Count > 10)   // 常磐線
                    {
                        strRouteDiff = String.Format(" or {0} in ('{1}', '{2}') and {3} = '{4}'",
                            RouteDataset.FLD_TSUUSHO_CD, "462", "463", RouteDataset.FLD_FEATURE_CD, "0302");
                    }
                    else if (routeInfo.AreaID.Contains("CL"))   // 中央・総武緩行線
                    {
                        if (objDVs[i].TableName.Contains("東京"))  // キロ標_東京
                        {
                            strRouteDiff = String.Format("({0} = '{4}' and {1} = '{2}' and {3} in ({5})) "
                                + " or ({0} = '{6}' and {1} = '{2}' and {3} in ({7}))",
                                RouteDataset.FLD_TSUUSHO_CD, RouteDataset.FLD_FEATURE_CD, "0302", RouteArea.FLD_KIROTEI,
                                "262", "12600,8200,2900",               // 御茶ノ水前
                                "651", "200,1000"); // 御茶ノ水(0)～錦糸町(5000)
                        }
                        else if (objDVs[i].TableName.Contains("千葉"))  // キロ標_千葉
                        {
                            strRouteDiff = String.Format("({0} = '{4}' and {1} = '{2}' and {3} in ({5})) "
                                + " or ({0} = '{6}' and {1} = '{2}' and {3} in ({7}))",
                                RouteDataset.FLD_TSUUSHO_CD, RouteDataset.FLD_FEATURE_CD, "0302", RouteArea.FLD_KIROTEI,
                                "651", "1500,1800,1900,2000",                   // 1500以降
                                "650", "19500,21800,37600,38000,38300,38800");  // 錦糸町(5000)以降
                        }
                    }

                    if (routeInfo.AreaID.Contains("KE")     // 京浜東北根岸線の場合
                        || routeInfo.AreaID.Contains("CL")) // 中央・総武緩行線の場合
                    {
                        objRSKirotei = objVector.Query(
                            strRouteDiff, true, null, 
                            "order by " + RouteDataset.FLD_TSUUSHO_CD + ", " + RouteArea.FLD_KIROTEI);
                    }
                    else
                    {
                        objRSKirotei = objVector.Query(
                            String.Format("({0} = '{1}'{2}) and {3} in ({4}) ",
                                RouteDataset.FLD_TSUUSHO_CD, strTsuusho, strRouteDiff, RouteArea.FLD_KIROTEI, strSqlKirotei),
                            true, null, "order by " + RouteDataset.FLD_TSUUSHO_CD + ", " + RouteArea.FLD_KIROTEI);
                    }
                    if (!strTsuusho.Equals(tsuusho))    // 特別な処理を入れたの後処理
                    {
                        strTsuusho = tsuusho;
                    }
                    if (objRSKirotei != null && objRSKirotei.RecordCount > 0)
                    {
                        if (!objRSKirotei.IsBOF())
                        {
                            objRSKirotei.MoveFirst();
                        }
                        ArrayList listKirotei = new ArrayList();
                        for (; !objRSKirotei.IsEOF(); objRSKirotei.MoveNext())
                        {
                            //キロ程は順序がないの場合
                            if (routeInfo.AreaID.Contains("YD")     // 山手線
                                || routeInfo.AreaID.Contains("SK")  // 横須賀線
                                || routeInfo.AreaID.Contains("TF")  // 東海道貨物線
                                || routeInfo.AreaID.Contains("KE")  // 京浜東北根岸線
                                || routeInfo.AreaID.Contains("NM")  // 成田(松岸)線
                                || routeInfo.AreaID.Contains("CL")  // 成田(松岸)線
                                || routeInfo.AreaID.Contains("JB") && multiRoutes.Count > 10) // 常磐線
                            {
                                ProcessMultiOrder(objPointsKirotei, routeInfo.AreaID.Substring(0, 3), multiRoutes, objRSKirotei /*, strSqlKirotei*/, objRoute);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("begin kirotei\t---" + Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI)));
                                int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                                foreach (RouteArea route in multiRoutes)
                                {
                                    RouteArea routeArea = route as RouteArea;
                                    if (bgnKirotei == routeArea.BeginKirotei)
                                    {
                                        Console.WriteLine(idx.ToString() + "\t---" + bgnKirotei.ToString() + "\t---" 
                                            + objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString());
                                        //キロ程がちょうど線路上の場合
                                        if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                                        {
                                            routeArea.BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                            if (listKirotei.Contains(bgnKirotei))
                                            {
                                                objPointsKirotei[listKirotei.IndexOf(bgnKirotei)] = routeArea.BeginPoint;
                                            }
                                            else
                                            {
                                                objPointsKirotei[idx++] = routeArea.BeginPoint;
                                                listKirotei.Add(bgnKirotei);
                                            }
                                            break;
                                        }
                                        //キロ程がずれ、線路の周辺に散らかるの場合
                                        if (!listKirotei.Contains(bgnKirotei))
                                        {
                                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < Utility.MAX_DISTANCE_TOLERANCE)
                                            {
                                                routeArea.BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                                objPointsKirotei[idx++] = routeArea.BeginPoint;
                                                listKirotei.Add(bgnKirotei);
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        objRSKirotei.Close();
                        Marshal.ReleaseComObject(objRSKirotei);
                        objRSKirotei = null;
                    }
                }
                //objVector.ClearRecordsets();
                //objVector.Close();
                //Marshal.ReleaseComObject(objVector);
                //objVector = null;

                if (multiRoutes.Count > 0)
                {
                    routeFtrWithInfo[feature + daibun + tsuusho] = multiRoutes;
                }
            }

            objVectorArea.ClearRecordsets();
            objVectorArea.Close();
            Marshal.ReleaseComObject(objVectorArea);
            objVectorArea = null;
        }

        /// <summary>
        /// キロ程は順序がないの線路に対して、個別的な処理を入る
        /// </summary>
        /// <param name="objPointsKirotei">分けるようにキロ程ポイント集合</param>
        /// <param name="strRoutePrefix">線路識別子</param>
        /// <param name="multiRoutes">複数ラインで構成した線路</param>
        /// <param name="objRSKirotei">通じているキロ程データセットベクトル</param>
        /// <param name="objRoute">対象線路</param>
        private void ProcessMultiOrder(soGeoPoint[] objPointsKirotei, string strRoutePrefix, ArrayList multiRoutes, soRecordset objRSKirotei /*, string strKirotei*/, soGeoLine objRoute)
        {
            string[] strKiroteis = new string[3];
            if (strRoutePrefix.Contains("YD"))
            {
                #region 山手線
                strKiroteis[0] = ",400,2600,2800,3600,8500,8600,13100,13700,17000,18800,19200,19300,20400,";
                strKiroteis[1] = ",6600,6000,5000,3200,3000,2100,800,700,";
                strKiroteis[2] = ",1300,5000,5600,";

                for (int i = 22; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "116")   // 東京起点（東海道電車線）
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                            + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (strKiroteis[2].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 2.5D)
                            {
                                if (bgnKirotei == 5600)
                                {
                                    (multiRoutes[0] as RouteArea).AreaID = strRoutePrefix + "1";
                                    (multiRoutes[0] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                    (multiRoutes[0] as RouteArea).BeginKirotei = bgnKirotei;
                                    (multiRoutes[multiRoutes.Count - 1] as RouteArea).EndKirotei = bgnKirotei;
                                    objPointsKirotei[0] = (multiRoutes[0] as RouteArea).BeginPoint;
                                    Console.WriteLine(String.Format("x={0}, y={1}",
                                        (multiRoutes[0] as RouteArea).BeginPoint.x,
                                        (multiRoutes[0] as RouteArea).BeginPoint.y));
                                }
                                else
                                {
                                    (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                    (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                    (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                    //(multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                    objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                    Console.WriteLine(String.Format("x={0}, y={1}",
                                        (multiRoutes[i] as RouteArea).BeginPoint.x,
                                        (multiRoutes[i] as RouteArea).BeginPoint.y));
                                }
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                        }
                    }
                    objRSKirotei.MoveNext();
                }
                objRSKirotei.MoveFirst();
                for (int i = 21; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "462")   // 東京起点（東北電車線）
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                            + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (strKiroteis[1].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 13D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                if (i < 21)
                                {
                                    (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                }
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                Console.WriteLine(String.Format("x={0}, y={1}",
                                    (multiRoutes[i] as RouteArea).BeginPoint.x,
                                    (multiRoutes[i] as RouteArea).BeginPoint.y));
                                i--;
                            }
                        }
                    }
                    objRSKirotei.MoveNext();
                }
                objRSKirotei.MoveFirst();
                for (int i = 1; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "475")
                        //&& objRSKirotei.GetFieldValueText(RouteDataset.FLD_FEATURE_CD) == "0302")   // 品川起点
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                            + bgnKirotei.ToString());
                        //if (//&& Array.IndexOf(strKiroteis, bgnKirotei) > 0
                            //objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (strKiroteis[0].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 6D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                Console.WriteLine(String.Format("x={0}, y={1}",
                                    (multiRoutes[i] as RouteArea).BeginPoint.x,
                                    (multiRoutes[i] as RouteArea).BeginPoint.y));
                                i++;
                            }
                        }
                    }
                    objRSKirotei.MoveNext();
                }
                #endregion
            }
            else if (strRoutePrefix.Contains("SK"))
            {
                #region 横須賀線
                int idx = 0;
                strKiroteis[0] = ",6200,400,";
                strKiroteis[1] = ",400,2800,3400,3900,8500,9100,9500,12400,12800,15100,16500,16600,23300,23500,24200,24400,28500,30200,34900,43400,";
                strKiroteis[2] = ",1400,1700,";

                if ((multiRoutes[1] as RouteArea).BeginPoint == null)
                {
                    idx = 1;
                }
                else
                {
                    idx = 7;
                }
                for (int i = idx; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "155")   // 品川起点
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                             + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (strKiroteis[1].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 4D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                        }
                    }
                    objRSKirotei.MoveNext();
                }
                objRSKirotei.MoveFirst();
                for (int i = 0; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "170")   // 東京（地下）起点
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                            + bgnKirotei.ToString());
                        if (strKiroteis[0].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 4D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                if (i > 0)
                                {
                                    (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                }
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                        }
                    }
                    objRSKirotei.MoveNext();
                }
                objRSKirotei.MoveFirst();
                for (int i = 21; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "175")   // 大船起点
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                             + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (strKiroteis[2].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 4D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                        }
                    }
                    objRSKirotei.MoveNext();
                }
                (multiRoutes[multiRoutes.Count - 1 - 1] as RouteArea).EndKirotei = Utility.FLAG_LAST_STOP;
                #endregion
            }
            else if (strRoutePrefix.Contains("TF"))
            {
                #region 東海道貨物線
                strKiroteis[0] = ",10700,12400,12800,15100,16500,16700,";   // 10400→10700に一次変換
                strKiroteis[1] = ",1700,3400,4900,12500,";
                strKiroteis[2] = ",43400,47900,56900,59900,";

                for (int i = 10; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "111")   // 東京起点
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                             + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (strKiroteis[2].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 10D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                        }
                    }
                    objRSKirotei.MoveNext();
                }
                objRSKirotei.MoveFirst();
                for (int i = 6; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "118")   // 鶴見起点
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                            + bgnKirotei.ToString());
                        if (strKiroteis[1].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 4D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                if (i > 0)
                                {
                                    (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                }
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                        }
                    }
                    objRSKirotei.MoveNext();
                }
                objRSKirotei.MoveFirst();
                for (int i = 0; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "155")   // 品川起点
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                             + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (strKiroteis[0].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) > 40D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                soRecordset objRSTmp = 
                                    (objDSs["キロ標_横浜"] as soDatasetVector).QueryByDistance(
                                        objRoute as soGeometry, 80D, 
                                        String.Format("{0} = '{1}' and {2} = '{3}' and {4} = {5}",
                                            RouteDataset.FLD_FEATURE_CD, "0302", 
                                            RouteDataset.FLD_TSUUSHO_CD, "115", 
                                            RouteArea.FLD_KIROTEI, "16700"));
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSTmp.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                //if (i > 0)
                                //{
                                    (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                //}
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSTmp.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                                Console.WriteLine(objRoute.DistanceTo(objRSTmp.GetGeometry()).ToString() + "\t---"
                                     + bgnKirotei.ToString());
                                objRSTmp.Close();
                                Marshal.ReleaseComObject(objRSTmp);
                                objRSTmp = null;
                            }
                            else if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < (i == 0 ? 4D : 40D))
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                if (i > 0)
                                {
                                    (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                }
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                        }
                    }
                    objRSKirotei.MoveNext();
                }
                (multiRoutes[multiRoutes.Count - 1] as RouteArea).EndKirotei = Utility.FLAG_LAST_STOP;
                #endregion
            }
            else if (strRoutePrefix.Contains("KE"))
            {
                #region 京浜東北根岸線
                int idx = 0;
                strKiroteis[0] = ",29200,27600,25200,23800,21400,15000,10400,9000,8200,7300,6600,6000,5000,3200,3000,2100,800,700,";
                strKiroteis[1] = ",1300,5000,5600,7200,9200,9800,9900,16900,18300,19000,20400,20600,23300,23500,24200,24400,28500,";
                strKiroteis[2] = ",1400,19200,";

                if ((multiRoutes[7] as RouteArea).BeginPoint == null)         // キロ程(大宮)15000以降
                {
                    idx = 7;

                    soGeoPoint objPointBegin = null;
                    soPoint tmpPoint = null;

                    ///*始端処理*/
                    //// 京浜東北線の始端　大宮（線路末端）
                    //tmpPoint = objRoute.FindPointOnLineByDistance(0D);
                    ////Console.WriteLine("x:" + tmpPoint.x.ToString() + "\t" + "y:" + tmpPoint.y.ToString());
                    //objPointBegin = new soGeoPoint().Clone();
                    //objPointBegin.x = tmpPoint.x - 1;
                    //objPointBegin.y = tmpPoint.y;
                    //(multiRoutes[0] as RouteArea).BeginPoint = objPointBegin;
                    //(multiRoutes[0] as RouteArea).AreaID = strRoutePrefix + "1";
                    //(multiRoutes[0] as RouteArea).BeginKirotei = 9;
                    //objPointsKirotei[0] = objPointBegin;


                    /*30300の処理*/
                    soGeoPoint objPointKirotei = null;

                    // 京浜東北線の始端　大宮（線路末端）
                    tmpPoint = objRoute.FindPointOnLineByDistance(1D);
                    //Console.WriteLine("x:" + tmpPoint.x.ToString() + "\t" + "y:" + tmpPoint.y.ToString());
                    objPointBegin = new soGeoPoint();
                    objPointBegin.x = tmpPoint.x;
                    objPointBegin.y = tmpPoint.y;

                    // 30300のキロ程
                    objPointKirotei =
                        GetSpecifiedKirotei(objRoute, 100D, 30300, "キロ標_大宮", "0302", "461");
                    // 30300に真似た始端からのキロ程
                    tmpPoint = objRoute.FindPointOnLineByDistance(
                        objPointBegin.DistanceTo(objPointKirotei as soGeometry));
                    objPointKirotei.x = tmpPoint.x;
                    objPointKirotei.y = tmpPoint.y;

                    (multiRoutes[1] as RouteArea).BeginPoint = objPointKirotei;
                    (multiRoutes[1] as RouteArea).AreaID = strRoutePrefix + "2";
                    (multiRoutes[1] as RouteArea).BeginKirotei = 30300;
                    (multiRoutes[0] as RouteArea).EndKirotei = 30300;
                    objPointsKirotei[1] = objPointKirotei;

                    Marshal.ReleaseComObject(tmpPoint);
                    tmpPoint = null;
                    //Marshal.ReleaseComObject(objPointBegin);
                    //objPointBegin = null;
                }
                else if ((multiRoutes[19] as RouteArea).BeginPoint == null)   // キロ程(東京)700～赤羽
                {
                    idx = 19;
                }

                for (int i = idx; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "461")   // 東京起点（東北電車線）王子以降
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                            + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (i == 7)
                        {
                            bgnKirotei = 15000;
                            (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                            (multiRoutes[i] as RouteArea).BeginPoint =
                                GetSpecifiedKirotei(objRoute, 20D, bgnKirotei, "キロ標1000mP_東京", "0301", "461");
                            (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                            if (i > 0)
                            {
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                            }
                            objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                            Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                            //Console.WriteLine(String.Format("x={0}, y={1}",
                            //    (multiRoutes[i] as RouteArea).BeginPoint.x,
                            //    (multiRoutes[i] as RouteArea).BeginPoint.y));
                            i--;
                        }
                        else if (strKiroteis[0].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 17D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                if (i > 0)
                                {
                                    (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                }
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                //Console.WriteLine(String.Format("x={0}, y={1}",
                                //    (multiRoutes[i] as RouteArea).BeginPoint.x,
                                //    (multiRoutes[i] as RouteArea).BeginPoint.y));
                                i--;
                            }
                            objRSKirotei.MoveNext();
                        }
                    }
                    else if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == tsuusho)   // 東京起点（東北電車線）～王子463
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                            + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (i == 9 || i == 13 || i == 14 || i == 16)
                        {
                            int kirotei = 0;
                            switch (i)
                            {
                                case 16:    // 3000
                                    kirotei = 3000;
                                    break;
                                case 14:    // 5000
                                    kirotei = 5000;
                                    break;
                                case 13:    // 6000
                                    kirotei = 6000;
                                    break;
                                case 9:     // 9000
                                    kirotei = 9000;
                                    break;
                                default:
                                    break;
                            }

                            (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                            (multiRoutes[i] as RouteArea).BeginPoint =
                                GetSpecifiedKirotei(objRoute, 0D, kirotei, "キロ標1000mP_東京", "0301", tsuusho);   //463
                            (multiRoutes[i] as RouteArea).BeginKirotei = kirotei;
                            if (i > 0)
                            {
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = kirotei;
                            }
                            objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                            //Console.WriteLine(String.Format("x={0}, y={1}",
                            //    (multiRoutes[i] as RouteArea).BeginPoint.x,
                            //    (multiRoutes[i] as RouteArea).BeginPoint.y));
                            i--;
                        }
                        else if (strKiroteis[0].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 12D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                if (i > 0)
                                {
                                    (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                }
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                //Console.WriteLine(String.Format("x={0}, y={1}",
                                //    (multiRoutes[i] as RouteArea).BeginPoint.x,
                                //    (multiRoutes[i] as RouteArea).BeginPoint.y));
                                i--;
                            }
                            objRSKirotei.MoveNext();
                        }
                    }
                    else
                    {
                        objRSKirotei.MoveNext();
                    }
                }

                if ((multiRoutes[20] as RouteArea).BeginPoint == null)        // キロ程(東京)～蒲田18300
                {
                    idx = 20;
                }
                else if ((multiRoutes[28] as RouteArea).BeginPoint == null)   // キロ程(東京)～蒲田18300
                {
                    idx = 28;
                }
                else if ((multiRoutes[37] as RouteArea).BeginPoint == null)   // キロ程(横浜)19000以降
                {
                    idx = 37;
                }

                objRSKirotei.MoveFirst();
                for (int i = idx; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "117")   // 東京起点（東海道電車線）～横浜
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                            + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (i == 21 || i == 29)
                        {
                            int kirotei = 0;
                            switch (i)
                            { 
                                case 21:
                                    kirotei = 5000;
                                    break;
                                case 29:
                                    kirotei = 19000;
                                    break;
                                default:
                                    break;
                            }
                            (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                            (multiRoutes[i] as RouteArea).BeginPoint =
                                GetSpecifiedKirotei(objRoute, 
                                i == 21 ? 9D : 5D, kirotei,
                                i == 21 ? "キロ標1000mP_東京" : "キロ標1000mP_横浜", "0301", 
                                i == 21 ? "116" : "117");
                            (multiRoutes[i] as RouteArea).BeginKirotei = kirotei;
                            if (i > 0)
                            {
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = kirotei;
                            }
                            objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                            //Console.WriteLine(String.Format("x={0}, y={1}",
                            //    (multiRoutes[i] as RouteArea).BeginPoint.x,
                            //    (multiRoutes[i] as RouteArea).BeginPoint.y));
                            i++;
                        }
                        else if (strKiroteis[1].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 14D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                //Console.WriteLine(String.Format("x={0}, y={1}",
                                //    (multiRoutes[i] as RouteArea).BeginPoint.x,
                                //    (multiRoutes[i] as RouteArea).BeginPoint.y));
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                            objRSKirotei.MoveNext();
                        }
                    }
                    else if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "173")   // 横浜起点以降
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                            + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (strKiroteis[2].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 15D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                //Console.WriteLine(String.Format("x={0}, y={1}",
                                //    (multiRoutes[i] as RouteArea).BeginPoint.x,
                                //    (multiRoutes[i] as RouteArea).BeginPoint.y));
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                        }
                        objRSKirotei.MoveNext();
                    }
                    else
                    {
                        objRSKirotei.MoveNext();
                    }
                }
                #endregion
            }
            else if (strRoutePrefix.Contains("NM"))
            {
                #region 成田(松岸)線
                strKiroteis[0] = ",0,4300,11500,12100,";
                strKiroteis[1] = ",2000,3700,29100,32700,58900,";

                for (int i = 0 + 1; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "662")   // 佐倉起点
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                             + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (strKiroteis[0].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 10D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                if (i > 0)
                                {
                                    (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                }
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                        }
                    }
                    objRSKirotei.MoveNext();
                }
                objRSKirotei.MoveFirst();
                for (int i = 4; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "663")   // 成田起点
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                            + bgnKirotei.ToString());
                        if (i == 4)
                        {
                            bgnKirotei = 2000;
                            (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                            (multiRoutes[i] as RouteArea).BeginPoint =
                                GetSpecifiedKirotei(objRoute, 0D, bgnKirotei, "キロ標1000mP_千葉", "0301", "663");
                            (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                            (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                            objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                            Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                            i++;
                        }
                        else if (strKiroteis[1].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                            //if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 4D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                            objRSKirotei.MoveNext();
                        }
                        else
                        {
                            objRSKirotei.MoveNext();
                        }
                    }
                    else
                    {
                        objRSKirotei.MoveNext();
                    }
                }
                (multiRoutes[multiRoutes.Count - 1] as RouteArea).EndKirotei = Utility.FLAG_LAST_STOP;
                #endregion
            }
            else if (strRoutePrefix.Contains("JB"))
            {
                #region 常磐線
                int idx = 0;
                strKiroteis[0] = ",2100,3000,3200,5000,";
                strKiroteis[1] = ",100,800,1700,4800,17000,19400,20000,21900,23300,29700,35500,";
                strKiroteis[2] = ",96700,100400,";
                string strKirotei_1 = String.Empty;
                if ((multiRoutes[16] as RouteArea).BeginPoint == null)
                {
                    idx = 16;
                    strKirotei_1 = strKiroteis[2];
                }
                else if ((multiRoutes[5] as RouteArea).BeginPoint == null)
                {
                    idx = 5;
                    strKirotei_1 = strKiroteis[1];
                }

                for (int i = idx; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "481")   // 日暮里起点以降
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                             + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (strKirotei_1.Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 16D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                        }
                    }
                    objRSKirotei.MoveNext();
                }

                if ((multiRoutes[0 + 1] as RouteArea).BeginPoint == null)
                {
                    idx = 0 + 1;
                }
                objRSKirotei.MoveFirst();
                for (int i = idx; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) != "481")   // 日暮里起点以降
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                            + bgnKirotei.ToString());
                        if (i == 2 || i == 4)
                        {
                            bgnKirotei = i == 2 ? 3000 : 5000;
                            (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                            (multiRoutes[i] as RouteArea).BeginPoint =
                                GetSpecifiedKirotei(objRoute, 4D, bgnKirotei, "キロ標1000mP_東京", "0301", 
                                i == 2 ? "463" : "467");
                            (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                            (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                            objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                            Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                            i++;
                        }
                        else if (strKiroteis[0].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 
                                (bgnKirotei == 3200 ? 5D : 17D))
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                if (i > 0)
                                {
                                    (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                }
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                            objRSKirotei.MoveNext();
                        }
                        else
                        {
                            objRSKirotei.MoveNext();
                        }
                    }
                    else
                    {
                        objRSKirotei.MoveNext();
                    }
                }
                (multiRoutes[multiRoutes.Count - 1] as RouteArea).EndKirotei = Utility.FLAG_LAST_STOP;
                #endregion
            }
            else if (strRoutePrefix.Contains("CL"))
            {
                #region 中央・総武緩行線
                int idx = 0;
                strKiroteis[0] = ",12600,8200,2900,";
                strKiroteis[1] = ",200,1000,1500,1800,1900,2000,";
                strKiroteis[2] = ",19500,21800,37600,38000,38300,38800,";

                if ((multiRoutes[3] as RouteArea).BeginPoint == null)
                {
                    idx = 3;
                }

                for (int i = idx; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "262")   // 三鷹～御茶ノ水
                    {
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                             + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (strKiroteis[0].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 16D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                if (i > 0)
                                {
                                    (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                }
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i--;
                            }
                        }
                        objRSKirotei.MoveNext();
                    }
                    else if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "651"
                        && (multiRoutes[4] as RouteArea).BeginPoint == null)   // 三鷹～御茶ノ水
                    {
                        if (i < 3)
                        {
                            i = 4;
                        }
                        int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                             + bgnKirotei.ToString());
                        //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                        if (strKiroteis[1].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 16D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                if (i > 0)
                                {
                                    (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                }
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                            bgnKirotei = 1000;
                            (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                            (multiRoutes[i] as RouteArea).BeginPoint =
                                GetSpecifiedKirotei(objRoute, 10D, bgnKirotei, "キロ標1000mP_東京", "0301", "651");
                            (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                            (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                            objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                            Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                            i++;
                            objRSKirotei.MoveNext();
                        }
                        else
                        {
                            objRSKirotei.MoveNext(); 
                        }
                    }
                    else
                    {
                        objRSKirotei.MoveNext();
                    }
                    if (i == 6)
                    {
                        return;
                    }
                }

                if ((multiRoutes[10] as RouteArea).BeginPoint == null)
                {
                    idx = 10;
                }
                objRSKirotei.MoveFirst();
                for (int i = idx; !objRSKirotei.IsEOF(); )
                {
                    Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                    int bgnKirotei = Convert.ToInt32(objRSKirotei.GetFieldValue(RouteArea.FLD_KIROTEI));
                    if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "650")   // 錦糸町以降(5000)
                    {
                        if (i == 13)
                        {
                            bgnKirotei = 38000;
                            (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                            (multiRoutes[i] as RouteArea).BeginPoint =
                                GetSpecifiedKirotei(objRoute, 10D, bgnKirotei, "キロ標1000mP_千葉", "0301", "650");
                            (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                            (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                            objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                            Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                            i++;
                        }
                        if (strKiroteis[2].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 17D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                        }
                        objRSKirotei.MoveNext();
                    }
                    else if (objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "651")   // 御茶ノ水～錦糸町
                    {
                        Console.WriteLine(objRoute.DistanceTo(objRSKirotei.GetGeometry()).ToString() + "\t---"
                            + bgnKirotei.ToString());
                        if (i > 15 && (multiRoutes[6] as RouteArea).BeginPoint == null)
                        {
                            i = 6;
                        }
                        if (strKiroteis[1].Contains("," + bgnKirotei.ToString() + ","))
                        {
                            //if (objRoute.SpatialRelation.Contains(objRSKirotei.GetGeometry()))
                            if (objRoute.DistanceTo(objRSKirotei.GetGeometry()) < 17D)
                            {
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint = objRSKirotei.GetGeometry() as soGeoPoint;
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                            if (i == 9)
                            {
                                bgnKirotei = 2000;
                                (multiRoutes[i] as RouteArea).AreaID = strRoutePrefix + (i + 1).ToString();
                                (multiRoutes[i] as RouteArea).BeginPoint =
                                    GetSpecifiedKirotei(objRoute, 10D, bgnKirotei, "キロ標1000mP_千葉", "0301", "651");
                                (multiRoutes[i] as RouteArea).BeginKirotei = bgnKirotei;
                                (multiRoutes[i - 1] as RouteArea).EndKirotei = bgnKirotei;
                                objPointsKirotei[i] = (multiRoutes[i] as RouteArea).BeginPoint;
                                Console.WriteLine(i.ToString() + "\t---" + objRSKirotei.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD));
                                i++;
                            }
                            objRSKirotei.MoveNext();
                        }
                        else
                        {
                            objRSKirotei.MoveNext();
                        }
                    }
                    else
                    {
                        objRSKirotei.MoveNext();
                    }
                }
                (multiRoutes[multiRoutes.Count - 1] as RouteArea).EndKirotei = Utility.FLAG_LAST_STOP;
                #endregion
            }
        }

        /// <summary>
        /// 回帰で線路をエリアコードを基づくキロ程で分ける
        /// </summary>
        /// <param name="objLine">対象線路ライン</param>
        /// <param name="objPoints">分けるようキロ程ポイント</param>
        /// <param name="idx">分けるカウンター</param>
        /// <param name="lineCount">分けたラインの数量</param>
        private void MakeDividingBuffer(soGeoLine objLine, soGeoPoint[] objPoints, int idx, ref int lineCount)
        {
            soGeoLine[] objLines = new soGeoLine[2];
            soPoint tmpPoint = null;
            soGeoRegion objBuffer = null;

            tmpPoint = new soPoint();
            objLines[0] = new soGeoLine();
            objLines[1] = new soGeoLine();

            tmpPoint.x = objPoints[idx].x;
            tmpPoint.y = objPoints[idx].y;

            double tolerance = Utility.MAX_DISTANCE_TOLERANCE;
            switch (feature + tsuusho)
            {
                case "0106175": // 横須賀線
                    if (tolerance == Utility.MAX_DISTANCE_TOLERANCE)
                    {
                        tolerance = 4D;
                    }
                    break;
                case "0106118": // 東海道貨物線
                    if (tolerance == Utility.MAX_DISTANCE_TOLERANCE)
                    {
                        tolerance = 40D;
                    }
                    break;
                case "0102475": // 山手線
                    if (tolerance == Utility.MAX_DISTANCE_TOLERANCE)
                    {
                        tolerance = 13D;
                    }
                    break;
                case "0102463": // 京浜東北根岸線
                    if (tolerance == Utility.MAX_DISTANCE_TOLERANCE)
                    {
                        tolerance = 20D;
                    }
                    break;
                case "0102650":
                    if (tolerance == Utility.MAX_DISTANCE_TOLERANCE)
                    {
                        tolerance = objLine.Length < 80000 ?
                            10D             // 成田(松岸)線
                            : tolerance;    // 総武
                    }
                    break;
                case "0102481": // 常磐線
                    if (tolerance == Utility.MAX_DISTANCE_TOLERANCE)
                    {
                        tolerance = 17D;
                    }
                    break;
                case "0102260": // 中央・総武緩行線
                    if (tolerance == Utility.MAX_DISTANCE_TOLERANCE)
                    {
                        tolerance = 17D;
                    }
                    break;
                default:
                    break;
            }

            //Console.WriteLine(
            //    "line:" + objLine.Length.ToString() + "\n" +
            //    "lineXLen:" + objLine.FindPointOnLineByDistance(objLine.Length).x.ToString() + ", " +
            //    "lineYLen:" + objLine.FindPointOnLineByDistance(objLine.Length).y.ToString() + "\n" +
            //    "lineX0:" + objLine.FindPointOnLineByDistance(0).x.ToString() + ", " +
            //    "lineY0:" + objLine.FindPointOnLineByDistance(0).y.ToString() + "\n" +
            //    "x:" + tmpPoint.x.ToString() + ", " +
            //    "y:" + tmpPoint.y.ToString());
            bool isSplited = false;
            if (tolerance == Utility.MAX_DISTANCE_TOLERANCE)
            {
                if (objLine.Split(tmpPoint, objLines[0], objLines[1]))
                {
                    isSplited = true;
                }
                else if (objLine.SplitEx(tmpPoint, objLines[0], objLines[1], tolerance))
                {
                    isSplited = true;
                }
            }
            else if (objLine.SplitEx(tmpPoint, objLines[0], objLines[1], tolerance))
            {
                isSplited = true;
            }

            if (isSplited)
            {
                bool hasRoute = false;
                ArrayList multiRoutes =
                    routeFtrWithInfo[feature + daibun + tsuusho] as ArrayList;

                if (objPoints[idx + 1] == null)
                {
                    Console.WriteLine(
                        String.Format("{0}:{1},\t{2}:{3},\t{4}:{5}\t---{6}/{7}---\t{1},{3},{5}",
                            RouteDataset.FLD_FEATURE_CD, feature, RouteDataset.FLD_DAIBUN_CD, daibun, RouteDataset.FLD_TSUUSHO_CD, tsuusho,
                            ++lineCount, routeLnWithBfr.Count + 1));

                    objBuffer = objLines[0].SpatialOperator.Buffer2(
                        Utility.BUFFER_ALARM_DISTANCE_2, Utility.BUFFER_ALARM_DISTANCE_2, 0);

                    if (multiRoutes != null)
                    {
                        hasRoute = false;
                        foreach (RouteArea route in multiRoutes)
                        {
                            //if (routeLnWithBfr.ContainsKey(route))
                            //{
                            //    continue;
                            //}
                            soGeoPoint objPointTmp = route.BeginPoint;
                            if (objPointTmp != null)
                            {
                                if (objLines[0].SpatialRelation.Contains(objPointTmp as soGeometry)
                                    || objLines[0].DistanceTo(objPointTmp as soGeometry) < tolerance)
                                {
                                    routeLnWithBfr[route as RouteArea] = objBuffer;
                                    hasRoute = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (!hasRoute)
                    {
                        routeLnWithBfr[objLines[0]] = objBuffer;
                    }

                    Console.WriteLine(
                        String.Format("{0}:{1},\t{2}:{3},\t{4}:{5}\t---{6}/{7}---\t{1},{3},{5}",
                            RouteDataset.FLD_FEATURE_CD, feature, RouteDataset.FLD_DAIBUN_CD, daibun, RouteDataset.FLD_TSUUSHO_CD, tsuusho,
                            ++lineCount, routeLnWithBfr.Count + 1));

                    objBuffer = objLines[1].SpatialOperator.Buffer2(
                        Utility.BUFFER_ALARM_DISTANCE_2, Utility.BUFFER_ALARM_DISTANCE_2, 0);

                    if (multiRoutes != null)
                    {
                        hasRoute = false;
                        foreach (RouteArea route in multiRoutes)
                        {
                            //if (routeLnWithBfr.ContainsKey(route))
                            //{
                            //    continue;
                            //}
                            soGeoPoint objPointTmp = route.BeginPoint;
                            if (objPointTmp != null)
                            {
                                if (objLines[1].SpatialRelation.Contains(objPointTmp as soGeometry)
                                    || objLines[1].DistanceTo(objPointTmp as soGeometry) < tolerance)
                                {
                                    routeLnWithBfr[route as RouteArea] = objBuffer;
                                    hasRoute = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (!hasRoute)
                    {
                        routeLnWithBfr[objLines[1]] = objBuffer;
                    }
                }
                else
                {
                    Boolean pointContain = false;

                    for (int twoIdx = 0; twoIdx < 2; twoIdx++)
                    {
                        pointContain = false;
                        for (int i = idx + 1; objPoints[i] != null; i++)
                        {
                            if (objLines[twoIdx].SpatialRelation.Contains(objPoints[i] as soGeometry)
                                ||objLines[twoIdx].DistanceTo(objPoints[i] as soGeometry) < tolerance)
                            {
                                pointContain = true;
                                MakeDividingBuffer(objLines[twoIdx], objPoints, i, ref lineCount);
                                break;
                            }
                        }
                        if (!pointContain)
                        {
                            Console.WriteLine(
                                String.Format("{0}:{1},\t{2}:{3},\t{4}:{5}\t---{6}/{7}---\t{1},{3},{5}",
                                    RouteDataset.FLD_FEATURE_CD, feature, RouteDataset.FLD_DAIBUN_CD, daibun, RouteDataset.FLD_TSUUSHO_CD, tsuusho,
                                    ++lineCount, routeLnWithBfr.Count + 1));

                            objBuffer = objLines[twoIdx].SpatialOperator.Buffer2(
                                Utility.BUFFER_ALARM_DISTANCE_2, Utility.BUFFER_ALARM_DISTANCE_2, 0);

                            if (multiRoutes != null)
                            {
                                hasRoute = false;
                                foreach (RouteArea route in multiRoutes)
                                {
                                    if (feature + tsuusho == "0102662") // 成田(我孫子)線の場合
                                    {
                                        if (routeLnWithBfr.ContainsKey(route))
                                        {
                                            continue;
                                        }
                                    }
                                    soGeoPoint objPointTmp = route.BeginPoint;
                                    if (objPointTmp != null)
                                    {
                                        if (objLines[twoIdx].SpatialRelation.Contains(objPointTmp as soGeometry)
                                            || objLines[twoIdx].DistanceTo(objPointTmp as soGeometry) < tolerance)
                                        {
                                            routeLnWithBfr[route as RouteArea] = objBuffer;
                                            hasRoute = true;
                                            break;
                                        }
                                    }
                                }
                            }

                            if (!hasRoute)
                            {
                                routeLnWithBfr[objLines[twoIdx]] = objBuffer;
                            }
                        }
                    }
                }
            }

            Marshal.ReleaseComObject(tmpPoint);
            tmpPoint = null;
            //Marshal.ReleaseComObject(objBuffer);
            //objBuffer = null;
            //Marshal.ReleaseComObject(objLines[0]);
            //objLines[0] = null;
            //Marshal.ReleaseComObject(objLines[1]);
            //objLines[1] = null;
        }

        //private void SetBufferWithLine(ArrayList multiRoutes, bool hasRoute)
        //{ 
        //}

        /// <summary>
        /// 線路は順序がないの場合、指定したキロ程を見当たる
        /// </summary>
        /// <param name="objRoute">探す線路</param>
        /// <param name="distance">探す距離範囲</param>
        /// <param name="kirotei">どのキロ程</param>
        /// <param name="strParams">必要な情報：キロ程のデータセットベクトル；地物と通所の名称</param>
        /// <returns>見つかったキロ程ポイント</returns>
        private soGeoPoint GetSpecifiedKirotei(soGeoLine objRoute, double distance, int kirotei, params string[] strParams)
        {
            soRecordset objRSTmp = null;
            soGeoPoint objPointSpecified = null;

            objRSTmp = (objDSs[strParams[0]] as soDatasetVector).QueryByDistance(
                objRoute as soGeometry, distance,
                String.Format("{0} = '{1}' and {2} = '{3}' and {4} = {5}",
                    RouteDataset.FLD_FEATURE_CD, strParams[1],
                    RouteDataset.FLD_TSUUSHO_CD, strParams[2],
                    RouteArea.FLD_KIROTEI, kirotei));

            objPointSpecified = objRSTmp.GetGeometry() as soGeoPoint;
            Console.WriteLine(objRoute.DistanceTo(objRSTmp.GetGeometry()).ToString() + "\t---" + kirotei.ToString());
            objRSTmp.Close();
            Marshal.ReleaseComObject(objRSTmp);
            objRSTmp = null;

            return objPointSpecified;
        }

        #region IRoute メンバ

        public string Feature
        {
            get
            {
                return feature;
            }
        }

        public string Daibun
        {
            get
            {
                return daibun;
            }
        }

        public string Tsuusho
        {
            get
            {
                return tsuusho;
            }
        }

        public virtual void MakeBufferForRoute(soDatasetVector objVector)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}
